/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument.uimodels;

import java.util.HashSet;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;

import dgbgui.modeldocument.DGBModel;

@SuppressWarnings("serial")
public class DGBModelComboModel extends DefaultComboBoxModel {
    


    public DGBModelComboModel() {
        // Add the "no model"
        addElement(m_nullModel);
    }
    
    public DGBModel getSelected() {
        return ((DGBModelItem)getSelectedItem()).getModel();
    }
    
    public void updateModels(Set<DGBModel> models) {
        clear();
        // Add the new ones
        for ( DGBModel model : models ) {
            addElement(new DGBModelItem(model));
        }
    }
    
    public void clear() {
        // Remove all but the null model. We create a list of all items to
        // remove, and then remove then, so that the null model is never removed
        // Removing the null model leads to an inconsistent state as there
        // must always be a crrent model
        Set<DGBModelItem> toRemove = new HashSet<DGBModelItem>();
        
        for ( int i = 0; i < getSize(); ++i ) {
            if ( getElementAt(i) != m_nullModel ) {
                toRemove.add((DGBModelItem)getElementAt(i));
            }
        }
        
        for ( DGBModelItem item : toRemove ) {
            removeElement(item);
        }
    }
    
    
    DGBModelItem m_nullModel = new DGBModelItem(new DGBModel("No model")); 

}
